/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.admin.ui;

import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import org.springframework.richclient.table.support.AbstractObjectTable;
import reportcenter.admin.business.AdminReportDao;
import reportcenter.report.dominio.report.Report;

/**
 *
 * @author renan_luz
 */
public class AdminReportTable extends AbstractObjectTable {

    /** Mantém todos os dados do relatórios */
    private AdminReportDao dao;

    /**
     * Default constructor.
     */
    public AdminReportTable(AdminReportDao dao) {
        super("adminReports", new String[]{"title", "subtitle", "idD.uri", "query"});
        this.dao = dao;
    }

    @Override
    protected Object[] getDefaultInitialData() {
        return dao.getAll().toArray();
    }

    protected void configureTable(JTable table) {
        // Adjust the table column widths
        TableColumnModel tcm = table.getColumnModel();
        tcm.getColumn(0).setPreferredWidth(100);
        tcm.getColumn(1).setPreferredWidth(100);
        tcm.getColumn(2).setPreferredWidth(50);
        tcm.getColumn(3).setPreferredWidth(300);
    }

    public Report[] getSelectedReports() {
        int[] selected = getTable().getSelectedRows();
        Report[] reports = new Report[selected.length];
        for (int i = 0; i < selected.length; i++) {
            reports[i] = (Report) getTableModel().getElementAt(selected[i]);
        }
        return reports;
    }

    public Report getSelectedReport() {
        return (Report) getSelectedReports()[0];
    }
}
